package com.third.clock.manager;

import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.PostRequest;
import com.orhanobut.logger.Logger;
import com.third.clock.common.Contants;
import com.third.clock.common.LogUtils;


import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/7/21 0021.
 */

public class OkHttpClientManager implements IBaseManager{

    private static final String TAG="OkHttpClientManager";

    private Context mContext;

    public OkHttpClientManager(Context context){
        this.mContext=context;
    }

    @Override
    public void onCreate() {
        init();
    }

    @Override
    public void onDestroy() {

    }

    private OkHttpClient init(){
        OkHttpClient okHttpClient  = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
        return okHttpClient;

    }

    public void post(String url, Map<String, Object> map, final okHttpCallback mOkHttpCallback){
        PostRequest postRequest= OkGo.post(url).tag(this).cacheMode(CacheMode.DEFAULT);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            postRequest.params(entry.getKey(),String.valueOf(entry.getValue()));
            LogUtils.e("post",entry.getKey()+String.valueOf(entry.getValue()));
        }
        postRequest.execute(new StringCallback() {
            @Override
            public void onSuccess(com.lzy.okgo.model.Response<String> response) {
               if(mOkHttpCallback!=null) mOkHttpCallback.onResponse(response);
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<String> response) {
                super.onError(response);
                if(mOkHttpCallback!=null) mOkHttpCallback.onFailure(response);
            }

        });
    }



//    public void post(String url, Map<String, Object> map, final okHttpCallback mOkHttpCallback){
//        OkHttpClient okHttpClient=init();
//       //MediaType  设置Content-Type 标头中包含的媒体类型值
//
//        FormBody.Builder builder = new FormBody.Builder();
//        for (Map.Entry<String, Object> entry : map.entrySet()) {
//            builder.add(entry.getKey(),String.valueOf(entry.getValue()));
//            LogUtils.e("post",entry.getKey()+String.valueOf(entry.getValue()));
//        }
//       // RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=utf-8"), params);
//        RequestBody requestBody = builder.build();
//
//        Request request=null;
//        try {
//            request = new Request.Builder()
//                    .url(url)//请求的url
//                    .post(requestBody)
//                    .addHeader("Accept", "application/json;charset=utf-8")
//                    .addHeader("Content-Type", "application/json;charset=utf-8")
//                    .build();
//        }catch (Exception e){
//            Logger.d("post Exception="+e.toString());
//            return;
//        }
//
//        //创建/Call
//        Call call = okHttpClient.newCall(request);
//        //加入队列 异步操作
//        call.enqueue(new Callback() {
//            //请求错误回调方法
//            @Override
//            public void onFailure(Call call, IOException e) {
//                LogUtils.e(TAG,"连接失败 onFailure"+e.toString());
//                if(mOkHttpCallback!=null)mOkHttpCallback.onFailure(call,e);
//            }
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                LogUtils.e(TAG,"onResponse=");
//                if(mOkHttpCallback!=null)mOkHttpCallback.onResponse(call,response);
//            }
//        });
//    }


//    public void get(String url,final okHttpCallback mOkHttpCallback){
//        // step 1: 创建 OkHttpClient 对象
//        OkHttpClient okHttpClient = new OkHttpClient();
//
//        // step 2： 创建一个请求，不指定请求方法时默认是GET。
//        Request.Builder requestBuilder = new Request.Builder().url(url);
//        //可以省略，默认是GET请求
//        requestBuilder.method("GET",null);
//
//        // step 3：创建 Call 对象
//        Call call = okHttpClient.newCall(requestBuilder.build());
//
//        //step 4: 开始异步请求
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                LogUtils.e(TAG,"连接失败 onFailure"+e.toString());
//               if(mOkHttpCallback!=null) mOkHttpCallback.onFailure(call,e);
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                // TODO: 17-1-4 请求成功
//                //获得返回体
//                LogUtils.e(TAG,"onResponse=");
//                if(mOkHttpCallback!=null) mOkHttpCallback.onResponse(call,response);
//            }
//        });
//    }


    public interface okHttpCallback {
        void onFailure(com.lzy.okgo.model.Response<String> s);

        void onResponse(com.lzy.okgo.model.Response<String> s);

    }

}
